﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

public partial class controls_MenuTopBar : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadMenu();
    }

    private void LoadMenu()
    {
        StringBuilder strM = new StringBuilder();

        DataTable tblBreadCumb = createData().Tables[0];

        strM.Append(@"<ul class='nav-pills nav'>");
        
        //strM.Append(@"<li class='active'><a href='" + tblBreadCumb.Rows[i]["URL"].ToString() + "'><i class='glyphicon glyphicon-home'></i>" + tblBreadCumb.Rows[i]["NAME"].ToString() + "</a></li>");

        var parentIds = (from DataRow dRow in tblBreadCumb.Rows
                         where dRow["ParentID"].ToString() != "-1"
                         select new { col1 = dRow["ParentID"]}).Distinct();

        foreach (DataRow drP in tblBreadCumb.Rows)
        {
            if (drP["ParentID"].ToString() != "-1")
            {
                

            }
            else
            {

                if (drP["ID"].ToString() == "3")
                {
                    var drSubs = from menuSub in tblBreadCumb.AsEnumerable()
                                 where menuSub.Field<string>("ParentID") == "3"
                                 select menuSub;

                    strM.Append(@"<li class='dropdown'>");
                    strM.Append(@"<a href='#' class='dropdown-toggle' data-toggle='dropdown'>" + drP["NAME"].ToString() + " <span class='caret'></span></a>");
                    strM.Append(@"<ul class='dropdown-menu' role='menu'>");
                    foreach (var item in drSubs)
                    {
                        strM.Append(@"<li><a href='" + item["URL"].ToString() + "'>" + item["NAME"].ToString() + "</a></li>");
                    }
                    strM.Append("</ul>");
                    strM.Append(@"</li>");
                }
                //if (parentIds.Contains(drP["ID"]))
                //{
                //     var drSubs = from menuSub in tblBreadCumb.AsEnumerable()
                //                  where menuSub.Field<string>("ParentID") == drP["ID"].ToString()
                //                  select menuSub;

                //     strM.Append(@"<li class='dropdown'>");
                //     strM.Append(@"<a href='#' class='dropdown-toggle' data-toggle='dropdown'>" + drP["NAME"].ToString() + " <span class='caret'></span></a>");
                //     strM.Append(@"<ul class='dropdown-menu' role='menu'>");
                //     foreach (var item in drSubs)
                //     {
                //         strM.Append(@"<li><a href='" + item["URL"].ToString() + "'>" + item["NAME"].ToString() + "</a></li>");
                //     }
                //     strM.Append("</ul>");
                //     strM.Append(@"</li>");
                //}                               
                else
                {
                    
                    strM.Append(@"<li><a href='" + drP["URL"].ToString() + "'>" + drP["NAME"].ToString() + "</a></li>");   
                }
            }
        }
      
        strM.Append("</ul>");
        ltMenuBar.Text = strM.ToString();
        //<ul class="nav-pills nav">
        //                <li class="active"><a href="#"><i class="glyphicon glyphicon-home"></i>Home</a></li>
        //                <li><a href="#">Link</a></li>
        //                <li class="dropdown">
        //                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
        //                    <ul class="dropdown-menu" role="menu">
        //                        <li><a href="#">Another action</a></li>
        //                        <li><a href="#">Something else here</a></li>
        //                        <li class="divider"></li>
        //                        <li><a href="#">Separated link</a></li>
        //                        <li class="divider"></li>
        //                        <li><a href="#">One more separated link</a></li>
        //                    </ul>
        //                </li>
        //            </ul>
    }

    private DataSet createData()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("NAME");
        dt.Columns.Add("URL");
        dt.Columns.Add("ParentID");
        for (int i = 1; i < 5; i++)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = i.ToString();
            dr["NAME"] = i.ToString() + "NAME";
            dr["URL"] ="http://wwww.evn.com"+ i.ToString() + "NAME";
            dr["ParentID"] = -1;
            dt.Rows.Add(dr);
        }

        for (int i = 1; i < 5; i++)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = i.ToString();
            dr["NAME"] = i.ToString() + "MENUSUB";
            dr["URL"] = "http://wwww.evn.com" + i.ToString() + "MENUSUB";
            dr["ParentID"] = 3;
            dt.Rows.Add(dr);
        }


        ds.Tables.Add(dt);
        ds.AcceptChanges();

        return ds;
    }
}